<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物业名称">
        <el-select v-model="queryParams.pmid" @visible-change="prosList" clearable>
          <el-option v-for="item in this.properList" :key="item.pmid" :label="item.propertyName"
                     :value="item.pmid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车场名称">
        <el-select v-model="queryParams.chid" @visible-change="carsList" clearable>
          <el-option v-for="item in this.carList" :key="item.chid" :label="item.chname" :value="item.chid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆类型" >
        <el-select v-model="queryParams.ctid" clearable :disabled="queryParams.chid === null || queryParams.chid === '' || queryParams.chid === undefined">
          <el-option v-for="item in this.carTList" :key="item.carsType.ctid" :label="item.carsType.typeName" :value="item.carsType.ctid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="套餐名称" prop="mealName">
        <el-input
          v-model="queryParams.mealName"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:meal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:meal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:meal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:meal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mealList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="自增ID" align="center" prop="cmid" />
      <el-table-column label="套餐名称" align="center" prop="mealName" />
      <el-table-column label="套餐类型" align="center" prop="mealTypeName.typeName" />
      <el-table-column label="车场" align="center" prop="carsHome.chname" />
      <el-table-column label="生效时长" align="center" prop="validityTime" />
      <el-table-column label="销量" align="center" prop="salesVolume" />
      <el-table-column label="库存" align="center" prop="inventory" />
      <el-table-column label="真实价格" align="center" prop="realPrice" />
      <el-table-column label="原价" align="center" prop="originalPrice" />
      <el-table-column label="车辆类型" align="center" prop="carsType.typeName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:meal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:meal:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆套餐对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="唯一编码,前台使用" prop="code">
          <el-input v-model="form.code" placeholder="请输入唯一编码,前台使用" />
        </el-form-item>
        <el-form-item label="是否删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="套餐名称" prop="mealName">
          <el-input v-model="form.mealName" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="车场id" prop="chid">
          <el-input v-model="form.chid" placeholder="请输入车场id" />
        </el-form-item>
        <el-form-item label="生效时长" prop="validityTime">
          <el-input v-model="form.validityTime" placeholder="请输入生效时长" />
        </el-form-item>
        <el-form-item label="销量" prop="salesVolume">
          <el-input v-model="form.salesVolume" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="库存" prop="inventory">
          <el-input v-model="form.inventory" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="真实价格" prop="realPrice">
          <el-input v-model="form.realPrice" placeholder="请输入真实价格" />
        </el-form-item>
        <el-form-item label="原价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="车辆类型id" prop="ctid">
          <el-input v-model="form.ctid" placeholder="请输入车辆类型id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMeal, getMeal, delMeal, addMeal, updateMeal } from "@/api/modules/meal/meal";
import {listCt} from "@/api/info/ct";
import {listHome, listProperty} from "@/api/ruoyi-home/home";
export default {
  name: "Meal",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆套餐表格数据
      mealList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        mealName: null,
        mealType: null,
        chid: null,
        validityTime: null,
        salesVolume: null,
        inventory: null,
        realPrice: null,
        originalPrice: null,
        ctid: null,
      },
      // 表单参数
      form: {},
      // 下拉框数据
      properList: [],
      carList: [],
      carTList: [],
      // 表单校验
      rules: {
        code: [
          { required: true, message: "唯一编码,前台使用不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        deleted: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
        mealName: [
          { required: true, message: "套餐名称不能为空", trigger: "blur" }
        ],
        mealType: [
          { required: true, message: "套餐类型不能为空", trigger: "change" }
        ],
        chid: [
          { required: true, message: "车场id不能为空", trigger: "blur" }
        ],
        validityTime: [
          { required: true, message: "生效时长不能为空", trigger: "blur" }
        ],
        salesVolume: [
          { required: true, message: "销量不能为空", trigger: "blur" }
        ],
        inventory: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        realPrice: [
          { required: true, message: "真实价格不能为空", trigger: "blur" }
        ],
        originalPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" }
        ],
        ctid: [
          { required: true, message: "车辆类型id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.prosList();
    this.carsList();
  },
  methods: {
    /** 查询车辆套餐列表 */
    getList() {
      this.loading = true;
      listMeal(this.queryParams).then(response => {
        this.mealList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cmid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        mealName: null,
        mealType: null,
        chid: null,
        validityTime: null,
        salesVolume: null,
        inventory: null,
        realPrice: null,
        originalPrice: null,
        ctid: null,
        remark: null
      };
      this.resetForm("form");
    },
    //下拉框获取集合数据
    prosList() {
      listProperty().then(res => {
        this.properList = res.rows;
        this.queryParams.chid = null;
        this.queryParams.ctid = null;
      })
    },
    carsList() {
      listHome(this.queryParams).then(res => {
        this.carList = res.rows;
        this.queryParams.ctid = null;
        if (this.queryParams.chid !== null || this.queryParams.chid !== '' || this.queryParams.chid !== undefined) {
          this.carsType();
        }
      })
    },
    carsType() {
      listCt(this.queryParams).then(res=>{
        this.carTList = res.rows;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cmid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "新增套餐";
      this.$router.push({path: "addmeal",query:{title:this.title}});
      // this.reset();
      // this.open = true;
      // this.title = "添加车辆套餐";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      // this.reset();
      const cmid = row.cmid || this.ids
      getMeal(cmid).then(response => {
      //   this.form = response.data;
      //   this.open = true;
        this.title = "套餐修改";
        this.$router.push({path: "addmeal",query:{title:this.title,data:response.data}});
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cmid != null) {
            updateMeal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMeal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cmids = row.cmid || this.ids;
      this.$modal.confirm('是否确认删除车辆套餐编号为"' + cmids + '"的数据项？').then(function() {
        return delMeal(cmids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/meal/export', {
        ...this.queryParams
      }, `meal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
